package com.example.kingdee.block;

import cn.hutool.core.util.NumberUtil;
import com.example.kingdee.dto.AllSunDto;
import com.example.kingdee.mapper.TBxStMapper;
import com.example.kingdee.sqlTool.SqlTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AllSunBlock {

    @Autowired
    private TBxStMapper tBxStMapper;

    @Autowired
    private SqlTool sqlTool;

    //计算子级物料需求量
    public List<Map<String, Object>> calculatePostDemandQty(Map<String, Double> topItemHavaQty, AllSunDto allSunDto) {
        List<Map<String, Object>> itemList = new ArrayList<>();
        for (String itemId : topItemHavaQty.keySet()) {
            //查询第二层级的物料用量
            List<Map<String, Object>> postItemList = tBxStMapper.getPostItem(itemId);
            //填充子级物料的需求量
            for (Map<String, Object> map : postItemList) {
                Double calculateResult = Double.parseDouble(map.get("calculateResult").toString());
                map.put("demandQty", NumberUtil.mul(topItemHavaQty.get(itemId), calculateResult));
            }
            getPostItem(itemList, postItemList);
        }
        return itemList;
    }

    //递归计算子级物料需求量
    public void getPostItem(List<Map<String, Object>> itemList, List<Map<String, Object>> postItemList) {
        if (postItemList != null && postItemList.size() != 0){
            itemList.addAll(postItemList);
            for (Map<String, Object> postItem : postItemList) {
                Double demandQty = Double.parseDouble(postItem.get("demandQty").toString());
                //查询下级物料用量
                String itemId = postItem.get("itemId").toString();
                List<Map<String, Object>> postItems = tBxStMapper.getPostItem(itemId);
                //填充子级物料的需求量
                for (Map<String, Object> map : postItems) {
                    Double calculateResult = Double.parseDouble(map.get("calculateResult").toString());
                    map.put("demandQty", NumberUtil.mul(demandQty, calculateResult));
                }
                getPostItem(itemList, postItems);
            }
        }
    }

    //按物料id汇总需求量
    public Map<String, Double> getItemDemandQty(List<Map<String, Object>> paramList) {
        MultiValueMap<String, Map<String, Object>> itemIdClassFy = sqlTool.getClassFy(paramList, "itemId");
        //计算最高层级物料的需求量
        Map<String, Double> topItemHavaQty = new HashMap<>();
        for (String itemId : itemIdClassFy.keySet()) {
            double demandQty = 0;
            List<Map<String, Object>> itemList = itemIdClassFy.get(itemId);
            for (Map<String, Object> item : itemList) {
                demandQty = NumberUtil.add(demandQty, item.get("demandQty") == null ? 0 : Double.parseDouble(item.get("demandQty").toString()));
            }
            topItemHavaQty.put(itemId, demandQty);
        }
        return topItemHavaQty;
    }

    public void isComplete(Map<String, Double> postItemHavaQty, AllSunDto allSunDto) {
        //删除历史纪录
        tBxStMapper.deleteMoResult(allSunDto.getUserName());
        for (String itemId : postItemHavaQty.keySet()) {
            Double demandQty = postItemHavaQty.get(itemId);
            Double stockQty = tBxStMapper.getItemStockQty(itemId) == null ? 0 : tBxStMapper.getItemStockQty(itemId);
            if (demandQty.compareTo(stockQty) <= 0){
                tBxStMapper.insertMoResult(itemId, demandQty, stockQty, "齐套", allSunDto.getUserName());
            }else {
                tBxStMapper.insertMoResult(itemId, demandQty, stockQty, "缺料", allSunDto.getUserName());
            }
        }
    }
}
